package com.gzrhhscd.controller;

import com.gzrhhscd.Pojo.Result;
import com.gzrhhscd.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {
//    @PostMapping("/upload")
//    public Result upload(String name, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传：{}，{}，{}",name,age,image);
//        String originalFilename = image.getOriginalFilename();
//        int index=originalFilename.indexOf(".");
//        String extName=originalFilename.substring(index);
//        String newName= UUID.randomUUID()+extName;
//        log.info("新文件名：{}",newName);
//        image.transferTo(new File("C:\\log\\"+newName));
//        return Result.success();
//    }
    @Autowired
    private AliOSSUtils aliOSSUtils;
@PostMapping("/upload")
public Result upload(MultipartFile image) throws IOException {
    log.info("文件上传：{}",image.getOriginalFilename());
    //2.use aliossutils to upload file
    String url = aliOSSUtils.upload(image);
    log.info("文件上传完成，文件访问的url:{}",url);
    return Result.success(url);
}
}
